package com.tencreat.common.service.impl;


import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.enums.ImMessageType;
import com.tencreat.common.core.enums.TrueFalseType;
import com.tencreat.common.entity.ImMessage;
import com.tencreat.common.entity.ImSession;
import com.tencreat.common.entity.ImUserSession;
import com.tencreat.common.mapper.ImMessageMapper;
import com.tencreat.common.service.ImMessageService;
import com.tencreat.common.service.ImSessionService;
import com.tencreat.common.service.ImUserSessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ImMessageServiceImpl extends ServiceImpl<ImMessageMapper, ImMessage> implements ImMessageService {
    @Autowired
    ImSessionService sessionService;
    @Autowired
    ImUserSessionService userSessionService;

    @Override
    @Lock4j
    @Transactional
    public void saveMsg(ImMessage message) {
        if(message.getMessageType().getCode() < 10){
            // 对话消息
            save(message);
            // 会话
            sessionService.lambdaUpdate().eq(ImSession::getId, message.getSessionId())
                    .set(ImSession::getLastMsg, message.getId())
                    .update();

            // 发信人
            userSessionService.lambdaUpdate().eq(ImUserSession::getSessionId, message.getSessionId())
                    .eq(ImUserSession::getUserId, message.getFromUser())
                    .set(ImUserSession::getLastAck, message.getId())
                    .update();
            // 收信人
            userSessionService.lambdaUpdate().eq(ImUserSession::getSessionId, message.getSessionId())
                    .eq(ImUserSession::getUserId, message.getToUser())
                    .set(ImUserSession::getIsShow, TrueFalseType.TRUE)
                    .setSql("`unread` = `unread` + 1")
                    .update();
        }else if(message.getMessageType().equals(ImMessageType.READ)){
            // 已读消息
            // 发信人
            userSessionService.lambdaUpdate().eq(ImUserSession::getSessionId, message.getSessionId())
                    .eq(ImUserSession::getUserId, message.getFromUser())
                    .set(ImUserSession::getLastAck, message.getId())
                    .setSql("`unread` = `unread` - 1")
                    .update();

        }else if(message.getMessageType().equals(ImMessageType.RECALL)){
            // 撤回消息
            removeById(message);
            // 找到这个会话最新的一条消息
            ImMessage latestMsg = lambdaQuery().eq(ImMessage::getSessionId, message.getSessionId())
                    .orderByDesc(ImMessage::getId).one();
            // 更新用户状态为撤回前状态
            // 会话
            sessionService.lambdaUpdate().eq(ImSession::getId, message.getSessionId())
                    .set(message.getId() > latestMsg.getId(),ImSession::getLastMsg, latestMsg.getId())
                    .update();
            // 收信人 (如果收件人撤回消息未读，更新状态，  已读则不管)
            userSessionService.lambdaUpdate().eq(ImUserSession::getSessionId, message.getSessionId())
                    .eq(ImUserSession::getUserId, message.getToUser())
                    .lt(ImUserSession::getLastAck, latestMsg.getId())
                    .set(ImUserSession::getLastAck, latestMsg.getId())
                    .setSql("`unread` = `unread` + 1")
                    .update();

        }
    }

    @Override
    public Long maxMessageId() {
        return baseMapper.maxMessageId();
    }
}
